package com.easthome.controller;

import com.easthome.constant.MessageConstant;
import com.easthome.pojo.OrderSetting;
import com.easthome.result.Result;
import com.easthome.service.OrderSettingService;
import com.easthome.util.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * 预约设置的请求和响应
 */
@RestController
@RequestMapping("/orderSetting")
public class OrderSettingController {

    @Autowired
    private OrderSettingService orderSettingService;

    /**
     * 修改预约人数
     * @param date
     * @param number
     * @return
     */
    @GetMapping("/updateOrderSetting")
    public Result updateOrderSetting(String date,Integer number){
        orderSettingService.updateOrderSettingNumber(date,number);
        return  new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
    }

    /**
     * 接收当前月的请求参数
     * 查询本月的预约数据
     * @return
     */
    @GetMapping("/queryOrderSettingByMonth")
    public Result queryOrderSettingByMonth(String date){

        //调用业务层  传递日期 返回当前月地预约数据
        //要求业务层必须返回 List<Map<String,Integer>>
        List<Map<String,Integer>> orderSettings = orderSettingService.queryOrderSettingByMonth(date);
        System.out.println(orderSettings);
        return  new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,orderSettings);
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile) throws IOException {
        //调用excel工具类 返回类是集合
        List<String[]> strings = POIUtils.readExcel(excelFile);
        //创建存储java对象的机构和
        ArrayList<OrderSetting> orderSettings = new ArrayList<>();
        for (String[] string : strings) {
            //创建oederSetting对象
            OrderSetting orderSetting = new OrderSetting();
            //设置预约日期
            orderSetting.setOrderDate(new Date(string[0]));
            //设置可预约人数
            orderSetting.setNumber(Integer.parseInt(string[1]));
            orderSettings.add(orderSetting);

        }
        orderSettingService.addOrderSetting(orderSettings);
        return  null;
    }
}
